package com.scala.zhenghui

import scala.collection.immutable.Range.Inclusive
import scala.collection.mutable.ListBuffer

/**
 * Created by Administrator on 2018/1/30/030.
 */
object ListDemo {
  def main(args: Array[String]) {
    //
    val list1:List[Int] = List(1,2,3,4,5)
    println(list1)
    println(list1(3))
    //
    val list2 = 1::2::3::4::5::Nil
    println(list2)
    //
    val list3 = List(1,2,3):::List(4,5,6):::List(7,8)
    println(list3)
    println(list3.head)
    println(list3.tail)
    //
    val list4 = List(List(1,2,3),4)
    println(list4)
    println(list4.head)
    println(list4.tail)


    println(list3.mkString("[",":","]"))
    val list6 = list3.mkString("[",":","]")


    //可变长
    val buf1 =  ListBuffer[Int]()
    buf1 += 12
    buf1 += 22
    buf1 ++= List(1,23,4)
    buf1 += (8,9,10)

    println(buf1)
    buf1 --= List(1,23,4)
    println(buf1)
    buf1(1) = 100
    println(buf1)

    //4.类型转换
    buf1.toList
    buf1.toList.toBuffer
    buf1.toArray
    buf1.toSet
    //5.集合中常用API
    /**
     * 这部分的API要认真学习。
     *
     */
    //map def map[B, That](f: A => B)
    //map:循环遍历集合，将集合中的每一个参数传入，用户给定的匿名函数中做操作
    //一般来说map中是用作集合的元素的转换操作
    list1.map((x) => x * 2)
    list1.map((x) => {
      x * 2
    })
    list1.map(_* 2)
    //foreach
    list1.foreach(x => println(x*x))
    //flatmap扁平化操作
    //
    val list0 =  List(List(1,2,3),List(4,5,6),List(7,8,9))
    val mlist1 = list0.map((x) => x )
    println(mlist1)
    val flist1 =  list0.flatMap(x => x)
    list0.flatMap(x=>{x.map(x=>(0 to x))})


    //扁平化操作后每个X是个list 再用map把 list的每个元素转化为range
    list0.flatMap(x=>{x.map(x=> (new Inclusive(0 , x, 2)))})

    //filter 传入的匿名函数，如果返回值为TRUE就会将当前的值保留，否则就过滤
    list1.filter(x => x%2==0)

    val s: Inclusive = new Inclusive(0 , 10, 2)
    val s1: Inclusive = 0 to 10

  }
}
